/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.lealone.cluster.dht;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

import org.lealone.cluster.db.DecoratedKey;
import org.lealone.cluster.exceptions.ConfigurationException;
import org.lealone.cluster.utils.ByteBufferUtil;
import org.lealone.cluster.utils.MurmurHash;
import org.lealone.cluster.utils.ObjectSizes;

import com.google.common.primitives.Longs;

/**
 * This class generates a BigIntegerToken using a Murmur3 hash.
 */
public class Murmur3Partitioner implements IPartitioner {
    public static final LongToken MINIMUM = new LongToken(Long.MIN_VALUE);
    public static final long MAXIMUM = Long.MAX_VALUE;

    private static final int HEAP_SIZE = (int) ObjectSizes.measureDeep(MINIMUM);

    public static final Murmur3Partitioner instance = new Murmur3Partitioner();

    @Override
    public DecoratedKey decorateKey(ByteBuffer key) {
        return new DecoratedKey(getToken(key), key);
    }

    @Override
    public Token midpoint(Token lToken, Token rToken) {
        // using BigInteger to avoid long overflow in intermediate operations
        BigInteger l = BigInteger.valueOf(((LongToken) lToken).token), r = BigInteger
                .valueOf(((LongToken) rToken).token), midpoint;

        if (l.compareTo(r) < 0) {
            BigInteger sum = l.add(r);
            midpoint = sum.shiftRight(1);
        } else // wrapping case
        {
            BigInteger max = BigInteger.valueOf(MAXIMUM);
            BigInteger min = BigInteger.valueOf(MINIMUM.token);
            // length of range we're bisecting is (R - min) + (max - L)
            // so we add that to L giving
            // L + ((R - min) + (max - L) / 2) = (L + R + max - min) / 2
            midpoint = (max.subtract(min).add(l).add(r)).shiftRight(1);
            if (midpoint.compareTo(max) > 0)
                midpoint = min.add(midpoint.subtract(max));
        }

        return new LongToken(midpoint.longValue());
    }

    @Override
    public LongToken getMinimumToken() {
        return MINIMUM;
    }

    public static class LongToken extends Token {
        static final long serialVersionUID = -5833580143318243006L;

        final long token;

        public LongToken(long token) {
            this.token = token;
        }

        @Override
        public String toString() {
            return Long.toString(token);
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null || this.getClass() != obj.getClass())
                return false;

            return token == (((LongToken) obj).token);
        }

        @Override
        public int hashCode() {
            return Longs.hashCode(token);
        }

        @Override
        public int compareTo(Token o) {
            return Long.compare(token, ((LongToken) o).token);
        }

        @Override
        public IPartitioner getPartitioner() {
            return instance;
        }

        @Override
        public long getHeapSize() {
            return HEAP_SIZE;
        }

        @Override
        public Object getTokenValue() {
            return token;
        }
    }

    /**
     * Generate the token of a key.
     * Note that we need to ensure all generated token are strictly bigger than MINIMUM.
     * In particular we don't want MINIMUM to correspond to any key because the range (MINIMUM, X] doesn't
     * include MINIMUM but we use such range to select all data whose token is smaller than X.
     */
    @Override
    public LongToken getToken(ByteBuffer key) {
        if (key.remaining() == 0)
            return MINIMUM;

        long[] hash = new long[2];
        MurmurHash.hash3_x64_128(key, key.position(), key.remaining(), 0, hash);
        return new LongToken(normalize(hash[0]));
    }

    @Override
    public LongToken getRandomToken() {
        return new LongToken(normalize(ThreadLocalRandom.current().nextLong()));
    }

    private long normalize(long v) {
        // We exclude the MINIMUM value; see getToken()
        return v == Long.MIN_VALUE ? Long.MAX_VALUE : v;
    }

    @Override
    public boolean preservesOrder() {
        return false;
    }

    @Override
    public Map<Token, Float> describeOwnership(List<Token> sortedTokens) {
        Map<Token, Float> ownerships = new HashMap<Token, Float>();
        Iterator<Token> i = sortedTokens.iterator();

        // 0-case
        if (!i.hasNext())
            throw new RuntimeException("No nodes present in the cluster. Has this node finished starting up?");
        // 1-case
        if (sortedTokens.size() == 1)
            ownerships.put(i.next(), new Float(1.0));
        // n-case
        else {
            // used for addition later
            final BigInteger ri = BigInteger.valueOf(MAXIMUM).subtract(BigInteger.valueOf(MINIMUM.token + 1));
            final BigDecimal r = new BigDecimal(ri);
            Token start = i.next();
            BigInteger ti = BigInteger.valueOf(((LongToken) start).token); // The first token and its value
            Token t;
            BigInteger tim1 = ti; // The last token and its value (after loop)

            while (i.hasNext()) {
                t = i.next();
                ti = BigInteger.valueOf(((LongToken) t).token); // The next token and its value
                float age = new BigDecimal(ti.subtract(tim1).add(ri).mod(ri)).divide(r, 6, BigDecimal.ROUND_HALF_EVEN)
                        .floatValue(); // %age = ((T(i) - T(i-1) + R) % R) / R
                ownerships.put(t, age); // save (T(i) -> %age)
                tim1 = ti; // -> advance loop
            }

            // The start token's range extends backward to the last token, which is why both were saved above.
            float x = new BigDecimal(BigInteger.valueOf(((LongToken) start).token).subtract(ti).add(ri).mod(ri))
                    .divide(r, 6, BigDecimal.ROUND_HALF_EVEN).floatValue();
            ownerships.put(start, x);
        }

        return ownerships;
    }

    @Override
    public TokenFactory getTokenFactory() {
        return tokenFactory;
    }

    private final TokenFactory tokenFactory = new TokenFactory() {
        @Override
        public ByteBuffer toByteArray(Token token) {
            LongToken longToken = (LongToken) token;
            return ByteBufferUtil.bytes(longToken.token);
        }

        @Override
        public Token fromByteArray(ByteBuffer bytes) {
            return new LongToken(ByteBufferUtil.toLong(bytes));
        }

        @Override
        public String toString(Token token) {
            return token.toString();
        }

        @Override
        public void validate(String token) throws ConfigurationException {
            try {
                Long.valueOf(token);
            } catch (NumberFormatException e) {
                throw new ConfigurationException(e.getMessage());
            }
        }

        @Override
        public Token fromString(String string) {
            try {
                return new LongToken(Long.valueOf(string));
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException(String.format("Invalid token for Murmur3Partitioner. "
                        + "Got %s but expected a long value (unsigned 8 bytes integer).", string));
            }
        }
    };
}
